

export class ApiBase {
    protected getBaseUrl(defaultUrl: string): string {
        return defaultUrl
    }

    protected async transformResult<T>(url: string, response: Response, callback: (response: Response) => Promise<T>): Promise<T> {
        if (response.status === 401) {

            throw new Error("need login")
        }
        if (response.status === 400) {
            let apiResp = await response.json();
            if (Array.isArray(apiResp.error)) {
                // let msg = apiResp.error.map(x => `<p>${x.errorMessage}</p>`).join('');

            }
            let msg = apiResp['message'] || '400错误';
            console.error(apiResp);
            throw new Error(msg)
        }
        if (response.status === 500) {

            throw new Error('后台服务器错误')
        }

        let resp = await callback(response)

	/* 
        let isSuccess = await handlerResponse(resp)
        if (!isSuccess) {
            let msg = resp['message'] || '发生错误';
            console.error(resp);
            throw new Error(msg)
        }
	*/
        // if(!resp['isSuccess']){
        //   let err= new Error(resp['message']);
        //   err['data']=resp;
        //   throw err;
        // }
        return resp;

    }
    protected async transformOptions(request: RequestInit): Promise<RequestInit> {
        // request.credentials="include";

        request.mode = "cors";
        // request.headers["Authorization"] = "Bearer " + user.access_token;

        return request;
    }
}
